home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / jackobox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.1 KB  |  208 lines

  1. /*
  2.  * Copyright 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <GL/glx.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <X11/keysym.h>
  23.  
  24. static int      RGB_attributes[] =
  25. {
  26.     GLX_RGBA,
  27.     GLX_RED_SIZE, 1,
  28.     GLX_GREEN_SIZE, 1,
  29.     GLX_BLUE_SIZE, 1,
  30.     None,
  31. };
  32.  
  33. static float    rgbMap[8][3] =
  34. {
  35.     {1, 0, 0},
  36.     {0, 1, 0},
  37.     {1, 1, 0},
  38.     {0, 0, 1},
  39.     {1, 0, 1},
  40.     {0, 1, 1},
  41.     {1, 1, 1}
  42. };
  43.  
  44. static long     W = 650;
  45. static long     H = 300;
  46.  
  47. #define NUM_DESCENDENTS 7
  48.  
  49. Display        *dpy;
  50. int             nDescendants = NUM_DESCENDENTS;
  51. Window          descendant[NUM_DESCENDENTS];
  52. GC        gc;
  53. GLXContext      cx;
  54. XColor          black;
  55. int         colorBase = 0;
  56.  
  57. static void 
  58. FillWindow(int color)
  59. {
  60.     glClearColor(rgbMap[color][0],
  61.          rgbMap[color][1],
  62.          rgbMap[color][2], 0);
  63.     glClear(GL_COLOR_BUFFER_BIT);
  64. }
  65.  
  66. static void WindowPrint(Window win, int x, int y, char *message)
  67. {
  68.    XDrawString(dpy, win, gc, x, y, message, strlen(message));
  69. }
  70.  
  71. static char *text[] = {
  72. "This program creates a nest of seven",
  73. "single-buffered RGB windows.  A single",
  74. "OpenGL rendering context is bound to each",
  75. "window in turn and a clear is done to the",
  76. "window (each window cleared to a different",
  77. "color).  Finally, this message is drawn",
  78. "using X.  If you can't read this message,",
  79. "the program didn't work.",
  80. NULL
  81. };
  82.  
  83. static void 
  84. ClearWindows(void)
  85. {
  86.     Window win;
  87.     int             i;
  88.  
  89.     for (i = nDescendants - 1; i >= 0; i--) {
  90.     glXMakeCurrent(dpy, descendant[i], cx);
  91.     FillWindow((i + colorBase) % 7);
  92.     }
  93.     glFinish();
  94.     win = descendant[nDescendants - 1];
  95.     for(i=0; text[i] != NULL; i++) {
  96.        WindowPrint(win, 20, 20 + 20 * i, text[i]);
  97.     }
  98. }
  99.  
  100. static Bool 
  101. WaitForMapNotify(Display * d, XEvent * e, char *arg)
  102. {
  103.     if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
  104.     return GL_TRUE;
  105.     }
  106.     return GL_FALSE;
  107. }
  108.  
  109. int 
  110. main(int argc, char **argv)
  111. {
  112.     XVisualInfo    *vi;
  113.     Colormap        cmap;
  114.     Window          window;
  115.     XSetWindowAttributes swa;
  116.     XGCValues       gcvals;
  117.     XEvent          event;
  118.     GLboolean       needDisplay;
  119.     int             i;
  120.  
  121.     dpy = XOpenDisplay(0);
  122.     if (!dpy) {
  123.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  124.     return -1;
  125.     }
  126.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), RGB_attributes);
  127.     if (!vi) {
  128.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  129.         getenv("DISPLAY"));
  130.     return -1;
  131.     }
  132.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  133.                AllocNone);
  134.     black.red = 0;
  135.     black.green = 0;
  136.     black.blue = 0;
  137.     XAllocColor(dpy, cmap, &black);
  138.  
  139.     swa.border_pixel = 0;
  140.     swa.background_pixel = black.pixel;
  141.     swa.colormap = cmap;
  142.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  143.     | KeyReleaseMask;
  144.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  145.                W, H,
  146.                0, vi->depth, InputOutput, vi->visual,
  147.              CWBackPixel | CWBorderPixel | CWColormap | CWEventMask,
  148.                &swa);
  149.     gcvals.foreground = black.pixel;
  150.     gc = XCreateGC(dpy, window, GCForeground, &gcvals);
  151.     descendant[0] = window;
  152.     for (i = 1; i < nDescendants; i++) {
  153.     descendant[i] = XCreateWindow(dpy, descendant[i - 1], 10, 10,
  154.                       W - 20 * i, H - 20 * i,
  155.                       0, vi->depth, InputOutput, vi->visual,
  156.              CWBackPixel | CWBorderPixel | CWColormap | CWEventMask,
  157.                       &swa);
  158.     XMapWindow(dpy, descendant[i]);
  159.     }
  160.     XSetStandardProperties(dpy, window, "jackobox", "jackobox",
  161.                None, argv, argc, NULL);
  162.     XSetWMColormapWindows(dpy, window, &window, 1);
  163.     XMapWindow(dpy, window);
  164.     XIfEvent(dpy, &event, WaitForMapNotify, (char *) window);
  165.  
  166.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  167.     if (!glXMakeCurrent(dpy, window, cx)) {
  168.     fprintf(stderr, "Can't make window current to context\n");
  169.     return -1;
  170.     }
  171.     needDisplay = GL_TRUE;
  172.     for (;;) {
  173.     do {
  174.         XNextEvent(dpy, &event);
  175.         switch (event.type) {
  176.         case Expose:
  177.         needDisplay = GL_TRUE;
  178.         break;
  179.         case ConfigureNotify:
  180.         needDisplay = GL_TRUE;
  181.         break;
  182.         case KeyPress:
  183.         {
  184.            char buf[100];
  185.            int rv;
  186.            KeySym ks;
  187.  
  188.            rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  189.            switch (ks) {
  190.              case XK_space:
  191.             colorBase++;
  192.             needDisplay = GL_TRUE;
  193.             break;
  194.              case XK_Escape:
  195.                exit(0);
  196.            }
  197.         }
  198.         break;
  199.         }
  200.     } while (XPending(dpy) != 0);
  201.  
  202.     if (needDisplay) {
  203.         needDisplay = GL_FALSE;
  204.         ClearWindows();
  205.     }
  206.     }
  207. }
  208.